﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MD5生成器
{
    public partial class Form1 : Form
    {
        public string filePath;
        public Form1()
        {
            InitializeComponent();
        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.RootFolder = System.Environment.SpecialFolder.Desktop;
            fbd.ShowNewFolderButton = true;
            fbd.Description = "请选择目录";
            if (textBox1.Text != "" && textBox1.Text != null)
            {
                fbd.SelectedPath = textBox1.Text;
            }
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                filePath= fbd.SelectedPath.ToString();
                textBox1.Text = filePath;
            }
            
        }       

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != null && textBox3.Text != null && textBox1.Text != ""  && textBox3.Text != "")
            {
                string[] files = Directory.GetFiles(textBox1.Text, "*", SearchOption.AllDirectories);
                StringBuilder versions = new StringBuilder();
                for (int i = 0; i < files.Length; i++)
                {
                    string filePath = files[i];
                    string relativePath = filePath.Replace(textBox1.Text, "").Replace("\\", "/");
                    if (!filePath.Contains("Versions.txt") && !filePath.Contains("Update.exe"))
                    {
                        string md5 = getFileHash(filePath);
                        versions.Append(relativePath).Append(",").Append(md5).Append("\r\n");
                    }
                }
                string md5s = versions.ToString();
                Write(md5s, textBox1.Text+"\\MD5.txt");
                Write(textBox3.Text, textBox1.Text+ "\\Versions.txt");
                MessageBox.Show("md5与版本号已生成");
            }
            else
            {
                MessageBox.Show("有选项未选择！");
            }

        }
        public static string getFileHash(string filePath)
        {
            try
            {
                FileStream fs = new FileStream(filePath, FileMode.Open);
                int len = (int)fs.Length;
                byte[] data = new byte[len];
                fs.Read(data, 0, len);
                fs.Close();
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] result = md5.ComputeHash(data);
                string fileMD5 = "";
                foreach (byte b in result)
                {
                    fileMD5 += Convert.ToString(b, 16);
                }
                return fileMD5;
            }
            catch (FileNotFoundException e)
            {
                return "";
            }
        }

        public void Write(string md5,string path)
        {
            FileStream fs = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            sw.Write(md5);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }

       

        //private void button3_Click(object sender, EventArgs e)
        //{
        //    SaveFileDialog openFileDialog = new SaveFileDialog();
        //    if (textBox2.Text != "" && textBox2.Text != null)
        //    {
        //        openFileDialog.InitialDirectory = textBox2.Text;
        //    }
        //    else
        //    {
        //        openFileDialog.InitialDirectory = "c:\\";
        //    }
        //    openFileDialog.Filter = "Files (*.txt)|*.txt";
        //    openFileDialog.FilterIndex = 2;
        //    openFileDialog.RestoreDirectory = true;
        //    openFileDialog.FileName = "MD5";
        //    if (openFileDialog.ShowDialog() ==DialogResult.OK)
        //    {
        //        filePath = openFileDialog.FileName;
        //        textBox2.Text = filePath;
        //    }
        //}


       

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void label3_Click(object sender, EventArgs e)
        {

        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
